<template>
  <a-modal
    :title="title"
    width="40%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @cancel="
      () => {
        this.visible = false
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="收货单位" prop="ReceivingUnit">
          <a-input v-model="entity.ReceivingUnit" autocomplete="off" placeholder="收货单位" />
        </a-form-model-item>
        <a-form-model-item label="收件人" prop="Addressee">
          <a-input v-model="entity.Addressee" autocomplete="off" placeholder="收件人" />
        </a-form-model-item>
        <a-form-model-item label="联系电话" prop="TelNumber">
          <a-input v-model="entity.TelNumber" autocomplete="off" placeholder="联系电话" />
        </a-form-model-item>
        <a-form-model-item label="收件地址" prop="Address">
          <a-input v-model="entity.Address" autocomplete="off" placeholder="收件地址" />
        </a-form-model-item>
        <a-form-model-item label="备注" prop="Remark">
          <a-input v-model="entity.Remark" autocomplete="off" placeholder="备注" />
        </a-form-model-item>
        <a-form-model-item label="默认值" prop="IsDefault">
          <a-radio-group v-model="entity.IsDefault" :default-value="false" button-style="solid">
            <a-radio-button :value="1"> 是</a-radio-button>
            <a-radio-button :value="0"> 否</a-radio-button>
          </a-radio-group>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
export default {
  props: {
    parentObj: Object
  },
  data() {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      rules: {
        Addressee: [{ required: true, message: '未输入', trigger: 'blur' }],
        TelNumber: [{ required: true, message: '未输入', trigger: 'blur' }],
        Address: [{ required: true, message: '未输入', trigger: 'blur' }]
      },
      visible: false,
      loading: false,
      entity: {},
      title: ''
    }
  },
  methods: {
    init() {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    openForm(id, title, code) {
      this.init()
      this.title = title
      this.$set(this.entity, 'CustomerId', code)
      if (id) {
        this.loading = true
        this.$http.post('/CustomerManagement/Cust_AddressBook/GetTheData', { id: id }).then(resJson => {
          this.loading = false

          this.entity = resJson.Data
        })
      }
    },
    handleSubmit() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true
        this.$http.post('/CustomerManagement/Cust_AddressBook/SaveData', this.entity).then(resJson => {
          this.loading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false

            this.parentObj.BookgetDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    }
  }
}
</script>
